//发送对话
$(document).ready(function(){
  $('.write_box input').focus();
  autoWidth();
  $("#inputWords").keydown(function(event){
	  var code = event.keyCode;
		if(code==13){//回车
			up_say();//调用up_say函数
		}
  }
    );
//发送按钮处理
  $("#sendText").click(up_say);
  
//发送语音
  $("#wenwen").click(sendVoice);
  
});

function to_write(){
	    $('.wenwen_btn img').attr('src','images/jp_btn.png');
	    $('.wenwen_btn').attr('onclick','to_say()');
	    $('.write_box,.wenwen_help button').show();
	    $('.circle-button,.wenwen_help a').hide();
	    $('.write_box input').focus();
	    for_bottom();
	}
function to_say(){
	$('.write_list').remove();
    $('.wenwen_btn img').attr('src','images/yy_btn.png');
    $('.wenwen_btn').attr('onclick','to_write()');
    $('.write_box,.wenwen_help button').hide();
    $('.circle-button,.wenwen_help a').show();
}

function up_say(){
	$('.write_list').remove();
    var words = $('.write_box input').val(),
        str  = '<div class="question">';
        str += '<div class="heard_img right"><img src="images/nana.png"></div>';
        str += '<div class="question_text clear"><p>'+words+'</p><i></i>';
        str += '</div></div>';
        if(words == ''){
	        alert('请输入对话内容！');
	        $('.write_box input').focus();
	    }else{
	    	 $('.speak_box').append(str);
		        $('.write_box input').val('');
		        $('.write_box input').focus();
		        autoWidth();
		        for_bottom();
		      //ajax请求
				$.ajax({
					url:"toTalk",
					type:"post",
					data:{"words":words},
					dataType:"json",
					success:function(result){
							setTimeout(function(){
					            var ans  = '<div class="answer"><div class="heard_img left"><img src="images/nini.png"></div>';
					            	ans += '<div class="answer_text"><p>'+result.msg+'</p><i></i>';
					        		ans += '</div></div>';
					        	$('.speak_box').append(ans);						        	
					        	var speak_height = $('.speak_box').height();
					    		$('.speak_box,.speak_window').animate({scrollTop:speak_height},500);
								$('.write_box input').focus();
					        },1000);
					},
					error:function(){
						alert("对话发生异常");
					}
				});
	    }
}

function keyup(){
	var footer_height = $('.wenwen-footer').outerHeight(),
		text = $('.write_box input').val(),
		str = '<div class="write_list">'+text+'</div>';
	if (text == '' || text == undefined){
		$('.write_list').remove();
	}else{
		$('.wenwen-footer').append(str);
		$('.write_list').css('bottom',footer_height);
	}
}

var $wen  = document.getElementById('wenwen');
	

function sendVoice(){
      //ajax请求
		$.ajax({
			url:"toSendVoice",
			type:"post",
			data:{"words":""},
			dataType:"json",
			success:function(result){
				var  str  = '<div class="question">';
		        str += '<div class="heard_img right"><img src="images/nana.png"></div>';
		        str += '<div class="question_text clear"><p>'+result.reqWords+'</p><i></i>';
		        str += '</div></div>';
		        $('.speak_box').append(str);
	            var ans  = '<div class="answer"><div class="heard_img left"><img src="images/nini.png"></div>';
	            	ans += '<div class="answer_text"><p>'+result.repWords+'</p><i></i>';
	        		ans += '</div></div>';
	        	$('.speak_box').append(ans);						        	
	        	var speak_height = $('.speak_box').height();
	    		$('.speak_box,.speak_window').animate({scrollTop:speak_height},500);
			},
			error:function(){
				alert("对话发生异常");
			}
		});
}
	
function _touch_start(event){
    event.preventDefault();
    $('.wenwen_text').css('background','#c1c1c1');
    $('.wenwen_text span').css('color','#fff');
    $('.saying').show();
}

function _touch_end(event){
    event.preventDefault();
    $('.wenwen_text').css('background','#fff');
    $('.wenwen_text .circle-button').css('color','#666');    
    $('.saying').hide();
    var str  = '<div class="question">';
        str += '<div class="heard_img right"><img src="images/dglvyou.jpg"></div>';
        str += '<div class="question_text clear"><p>不好意思，我听不清！</p><i></i>';
        str += '</div></div>';
	$('.speak_box').append(str);
	for_bottom();
    setTimeout(function(){
    	var ans  = '<div class="answer"><div class="heard_img left"><img src="images/dglvyou.jpg"></div>';
        	ans += '<div class="answer_text"><p>我不知道你在说什么?</p><i></i>';
    		ans += '</div></div>';
    	$('.speak_box').append(ans);
		for_bottom();
    },1000);       
}

wen.addEventListener("touchstart", _touch_start, false);
wen.addEventListener("touchend", _touch_end, false);

function for_bottom(){
	var speak_height = $('.speak_box').height();
	$('.speak_box,.speak_window').animate({scrollTop:speak_height},500);
}

function autoWidth(){
	$('.question_text').css('max-width',$('.question').width()-60);
}
autoWidth();